<template>
    <div class="layout-route">
        <div class="box_left">
            <!-- 添加图标 -->
            <div style="width: 100%;text-align: right;padding: 10px;">
                <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="showDetailDialog({}, 'view')">批量添加</el-button>
            </div>
            <!-- 搜索栏 -->
            <div style="width: 100%;padding: 10px;">
                <el-input placeholder="输入批次号" v-model="batch">
                    <el-button slot="append" icon="el-icon-search" @click="getList">
                    </el-button>
                </el-input>
            </div>

            <div class="container">
                <div class="container_box">
                    <div style="width: 100%;display: flex;align-items: center;justify-content: space-between;">
                        批次号
                        <span>创建人</span>
                    </div>
                </div>
                <div @click="changetab(index, item)" v-for="item, index in BatchArray" :key="item.id"
                    :class="item.isActive == true ? 'active' : 'noactive'" class="container_box">
                    <div style="width: 100%;display: flex;align-items: center;justify-content: space-between;">
                        {{ item.batch }}
                        <span>{{ item.admin_name }}</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="box_right">
            <div class="card card-search">
                <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                    <el-table-column label="店铺" prop="shop_name" min-width="60" show-overflow-tooltip></el-table-column>
                    <el-form-item label="券码" prop="code">
                        <el-input v-model.trim="queryParams.code" placeholder="请输入券码" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <!-- <el-form-item label="月份" prop="date">
                        <el-date-picker type="month" v-model="queryParams.date" ></el-date-picker>
                    </el-form-item> -->
                    <el-form-item label=" ">
                        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <div class="card">
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                    </el-col>
                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
                <el-table v-loading="loading" :data="tableData" border script>
                    <el-table-column label="店铺" prop="shop_name" min-width="100" show-overflow-tooltip></el-table-column>
                    <el-table-column label="券码" prop="code" min-width="120" show-overflow-tooltip></el-table-column>
                    <el-table-column label="币种" prop="currency" min-width="60" show-overflow-tooltip></el-table-column>
                    <el-table-column label="金额" prop="balance" min-width="60" show-overflow-tooltip></el-table-column>
                    <el-table-column label="初始值" prop="initial_value" min-width="60" show-overflow-tooltip></el-table-column>
                    <!-- <el-table-column label="代金券编码" prop="last_characters" min-width="" show-overflow-tooltip></el-table-column> -->
                    <el-table-column label="创建时间" prop="created_at" min-width="140" show-overflow-tooltip></el-table-column>
                    <!-- <el-table-column label="修改时间" prop="updated_at" min-width="" show-overflow-tooltip></el-table-column> -->
                    <el-table-column label="类型" prop="type" min-width="80" show-overflow-tooltip></el-table-column>
                    <el-table-column label="状态" prop="status" min-width="80" show-overflow-tooltip></el-table-column>
                    <el-table-column label="批次号" prop="batch" min-width="80" show-overflow-tooltip></el-table-column>
                    <!-- <el-table-column label="操作" min-width="160" fixed="right">
                        <template slot-scope="scope">
                            <el-button el-button type="text" icon="el-icon-search" @click="showDetailDialog(scope.row, 'view')">查看详情</el-button>
                        </template>
                    </el-table-column> -->
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
            </div>
        </div>
        
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
        <!-- <feedback-dialog v-model="feedbackDialog.visible" v-if="feedbackDialog.visible" :params="feedbackDialog.data" @success="handleQuery"></feedback-dialog> -->
    </div>
</template>

<script>
import { queryGiftcardList, exportGiftcard,queryGiftcardbatchList } from '@/api/order'
import { getShopList } from '@/api/shop'
import { STATUS_LIST, FINANCIAL_STATUS, FULFILLMENT_STATUS } from './constant'
import { exportFile } from '@/utils/index'
import DetailDialog from './components/DetailDialog.vue'
import { parseTime } from '@/utils/quickadmin'
// import FeedbackDialog from './components/FeedbackDialog.vue'
export default {
    name: 'OrderList',
    components: { DetailDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            STATUS_LIST,
            FINANCIAL_STATUS,
            FULFILLMENT_STATUS,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                order_id: undefined,
                shop_id: undefined,
                name: undefined,
                financial_status: undefined,
                fulfillment_status: undefined,
                create_time: [],
                keyword: undefined,
            },
            detailDialog: {
                visible: false,
                data: {},
                type: ''
            },
            feedbackDialog: {
                visible: false,
                data: {},
            },
            shopList: [],
            batch: "",
            BatchArray: [
            {
                id: 0,
                batch: 'API记录',
                isActive: true,
                admin_name: '张恒'
            }],
            
        }
    },
    async created() {
        this.getbatchList()
        this.loading = true
        try {
            const result = await getShopList()
            this.shopList = result?.data?.list ?? []
        } catch (error) {
            console.log(error)
            this.loading = false
        }
        this.getList()
    },
    methods: {
        changetab(index, item) {
            this.BatchArray.forEach((item) => {
                item.isActive = false;
            })
            this.$nextTick(() => {
                this.BatchArray[index].isActive = true;
            });
            this.batch = item.batch
            this.getList();
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
                batch: this.batch,
            }
            exportGiftcard(qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        // showFeedbackDialog(row) {
        //     this.feedbackDialog.visible = true
        //     this.feedbackDialog.data = row
        // },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
                batch: this.batch,
            }
            if (qyparams.date !== undefined) {
                qyparams.date = parseTime(qyparams.date, 'y-m')
            }
            queryGiftcardList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },
        getbatchList() {
            queryGiftcardbatchList()
                .then(response => {
                    this.BatchArray = response.data.list
                })
                .finally(() => {
                    
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getbatchList()
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        
    },
}
</script>
<style scoped>
.title_box {
    width: 50%;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-right: 10px;
}

.layout-route {
    display: flex;
    align-items: center;
    justify-content: space-around;
}

.container_box {
    width: 100%;
    padding: 10px;
    height: 70px;
    font-size: 14px;
    transition: ease-in-out 0.2s;
}

.active {
    border-right: 1px solid #11A983;
    background: #b9e9dd;
    box-sizing: border-box;
    cursor: pointer;
}

.noactive {
    border-right: 1px solid #fff;
    cursor: pointer;
}

.container {
    width: 100%;
    padding: 10px;
}

.box_left {
    /* width: 300px; */
    flex: 0.36;
    background-color: #fff;
    border-radius: 5px;
    min-height: 800px;
}

.box_right {
    flex: 0.60;
    min-height: 800px;
    border-radius: 5px;
    background-color: #fff;
    padding-left: 0.04;
}

.el-icon-circle-plus-outline {
    font-size: large;
    color: #11A983;
    cursor: pointer;
}
</style>
